package com.sec.android.app.myfiles.feature;

import android.app.Activity;
import android.graphics.Rect;
import android.util.SparseArray;

import com.samsung.android.sdk.multiwindow.SMultiWindowActivity;
import com.sec.android.app.myfiles.feature.multiwindow.AbsMultiWindowImp;
import com.sec.android.app.myfiles.feature.multiwindow.MultiWindowImp;

import java.util.ArrayList;

public class MultiWindowMgr {
    public enum WindowState {
        PORTRAIT,
        LANDSCAPE,
        PORTRAIT_MULTIWINDOW,
        LANDSCAPE_MULTIWINDOW
    }

    public enum WindowZone {
        ZONE_A,
        ZONE_B,
        NONE
    }

    /**
     * Wrapper Interface of SMultiWindowActivity.StateChangeListener
     */
    public interface StateChangeListener {
        void onModeChanged();

        void onZoneChanged();

        void onSizeChanged(Rect rectInfo);
    }

    AbsMultiWindowImp mMultiWindowImp = null;
    Activity mActivity;

    private ArrayList<StateChangeListener> mStateChangeListenerList = new ArrayList<>();

    private static SparseArray<MultiWindowMgr> mMultiWindowMap = new SparseArray<>();

    public static MultiWindowMgr getInstance(Activity activity, int processId) {
        MultiWindowMgr instance = mMultiWindowMap.get(processId);
        if (instance == null) {
            instance = new MultiWindowMgr(activity);
            mMultiWindowMap.put(processId, instance);
        }

        return instance;
    }

    private MultiWindowMgr(Activity activity) {
        mActivity = activity;
        mMultiWindowImp = createmultiWindowImp();
    }

    private AbsMultiWindowImp createmultiWindowImp() {
        AbsMultiWindowImp ret = new MultiWindowImp(mActivity);
        return ret;
    }

    public static void clearInstance(int processId) {
        MultiWindowMgr instance = mMultiWindowMap.get(processId);
        if (instance != null) {
            mMultiWindowMap.remove(processId);
        }
    }

    public WindowState getWindowState() {
        return mMultiWindowImp.getWindowState();
    }

    public boolean isMultiWindow() {
        return mMultiWindowImp.isMultiWindow();
    }

    public boolean addStateChangeListener(StateChangeListener listener) {
        return mStateChangeListenerList.add(listener);
    }

    public boolean removeStateChangeListener(StateChangeListener listener) {
        return mStateChangeListenerList.remove(listener);
    }

    public void clearStateChangeListener() {
        mStateChangeListenerList.clear();
    }

    public boolean isScaleWindow() {
        return mMultiWindowImp.isScaleWindow();
    }

    public Rect getRectInfo() {
        return mMultiWindowImp.getRectInfo();
    }

    SMultiWindowActivity.StateChangeListener mStateChangeListener = new SMultiWindowActivity.StateChangeListener() {
        @Override
        public void onModeChanged(boolean isMultiWindow) {
            for (StateChangeListener listener : mStateChangeListenerList) {
                listener.onModeChanged();
            }
        }

        @Override
        public void onSizeChanged(Rect rectInfo) {
            for (StateChangeListener listener : mStateChangeListenerList) {
                listener.onSizeChanged(rectInfo);
            }
        }

        @Override
        public void onZoneChanged(int zoneInfo) {
            for (StateChangeListener listener : mStateChangeListenerList) {
                listener.onZoneChanged();
            }
        }
    };
}
